package com.example.examplemod;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Arrow;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.w3c.dom.events.Event;
public class RightClick {
@SubscribeEvent
public static void RightClick(PlayerInteractEvent.RightClickItem event) {
if (!event.getLevel().isClientSide) {
Player player = event.getEntity();
Component rightClickMessage = Component.literal("RightClick");
player.sendSystemMessage(rightClickMessage);
Component leverMessage = Component.literal("This is a Lever");
ItemStack HeldItem = player.getItemInHand(InteractionHand.MAIN_HAND);
if (!HeldItem.isEmpty() && HeldItem.getItem() == Items.LEVER) {
player.sendSystemMessage(leverMessage);
double x = player.getX();
double y = player.getY()+player.getEyeHeight();
double z = player.getZ();
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
Arrow arrow = new Arrow(player.level, x, y, z);
arrow.shootFromRotation(player, player.getXRot()+i*5,player.getYRot()+j*5, 1F,5F, 1F);
player.level.addFreshEntity(arrow);
}
}
}
}
}
}
些微修改後的完整程式碼
Minecraft的事件偵測功能是很強大的,目前一個右鍵偵測就可以做到許多不同的功能,如果我將右鍵功能的目標從控制桿移至我自己的模組物品,再將這個射出的彈藥改成自製的投射物,可以做成一把槍枝之類的。
接下來的十天我們應該要做出
public static final DeferredRegister<Block> BLOCKS =
DeferredRegister.create(ForgeRegistries.BLOCKS, Mymod.MOD_ID);
public static final RegistryObject<Block> SILVER_BLOCK = registerBlock("silver_block",
()-> new Block(BlockBehaviour.Properties.of(Material.METAL).
strength(6f).requiresCorrectToolForDrops()));
這部分的程式碼與物品註冊的部分有意點不同,首先在
()-> new Block(BlockBehaviour.properties.of(Material.METAL))
這個部分是設定方塊的種類,這會影響方塊的指定破壞工具(如果有設定要指定工具的話),還有破壞的音效等等。
剩下的明天說